How to split form background color in C Sharp
This will change Windows form back color according to Button Click.
Button Red: btnRed
Blue: btnBlue
Split: btnSplit
Reset: btnReset
Code
bool check; //boolean variable to check whether split is clicked or not.
//function to split form background color
private void fnSplit()
{
Graphics surface = this.CreateGraphics();
surface.FillRectangle(new SolidBrush(Color.Red), 0, 0, (this.Width) / 2, this.Height);
//drawing filled rectangle of red color on form left half side with width equal to half of form
//width andheigth equalto the height of form.
surface.FillRectangle(new SolidBrush(Color.Blue), (this.Width) / 2, 0, (this.Width) / 2, this.Height);
//drawing another rectanfle of blue color on other hand of the form of same dimension as that of firstrectangle.
}
//changing form background color to red on Red button click
private void btnRed_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
//setting background color to red.
check = false;
}
//changing form background color to blue on Blue button click
private void btnBlue_Click(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
//setting background color to red.
check = false;
}
//spliting form background color on Split button click
private void btnSplit_Click(object sender, EventArgs e)
{
fnSplit();
//Calling function to split form color.
check = true;
//assigning true to check.
}
private void frmColor_SizeChanged(object sender, EventArgs e)
{
if(check)
fnSplit();
//calling fnSplit() function is form background color is already split on form resize event.
}
//this is reset the form bolor to its default color.
private void btnReset_Click(object sender, EventArgs e)
{
this.BackColor = DefaultBackColor;
if (check)
{
this.Refresh();
this.BackColor = DefaultBackColor;
}
}
Anonymous User
27-Feb-2019thanks.
Sushant Mishra
28-Jun-2017It was really helpful to read this post.